#ifndef METADATA_H_
#define METADATA_H_

#include <qstring.h>

#include <utility>
#include <vector>

#include "parentalcontrols.h"

class MSqlQuery;
class MetadataListManager;

enum { PROGRAMME_YEAR_DEFAULT = 1895 };

struct SortData;

class Metadata
{
  public:
    typedef std::pair<int, QString> genre_entry;
    typedef std::vector<genre_entry> genre_list;

  public:
    class SortKey
    {
      public:
        SortKey();
        SortKey(const SortData &data);
        SortKey(const SortKey &other);
        SortKey &operator=(const SortKey &rhs);
        ~SortKey();

        bool isSet() const;
        void Clear();

      public:
        SortData *m_sd;
    };

  public:
    static SortKey GenerateDefaultSortKey(const Metadata &m, bool ignore_case);
    static QString FilenameToTitle(const QString &file_name);
    static QString trimTitle(const QString &title, bool ignore_case);
    static QString getPlayer(const Metadata *item);
    static QString getPlayer(const Metadata *item, QString &internal_mrl);
    static QString getPlayCommand(const Metadata *item);

  public:
    Metadata(
              const QString &url = "",
              const QString &filename = "",
              const QString &coverfile = "",
              const QString &title = "",
              const QString &inetref = "",
              const QString &description = "",
              const QString &series = "",
              float userrating = 0.0,
              const QString &rating = "",
              int id = 0,
              ParentalLevel::Level showlevel = ParentalLevel::plLowest,
              const QString &playcommand = "",
              const genre_list &genres = genre_list()
              );
    ~Metadata();
    Metadata(MSqlQuery &query);
    Metadata(const Metadata &rhs);
    Metadata &operator=(const Metadata &rhs);

    // returns a string to use when sorting
    bool hasSortKey() const;
    const SortKey &getSortKey() const;
    void setSortKey(const SortKey &sort_key);

    // flat index
    void setFlatIndex(int index);
    int getFlatIndex() const;

    const QString &getPrefix() const;
    void setPrefix(const QString &prefix);

    const QString &URL() const;
    void setURL(const QString& url);

    const QString &Title() const;
    void setTitle(const QString& title);

    const QString &Series() const;
    void setSeries(const QString& series);

    const QString &InetRef() const;
    void setInetRef(const QString &inetRef);

    const QString &Description() const;
    void setDescription(const QString &description);

    float UserRating() const;
    void setUserRating(float userRating);

    const QString &Rating() const;
    void setRating(const QString &rating);

    unsigned int ID() const;
    void setID(int id);

    const QString &PlayCommand() const;
    void setPlayCommand(const QString &playCommand);

    ParentalLevel::Level ShowLevel() const;
    void setShowLevel(ParentalLevel::Level showLevel);

    const QString& Filename() const;
    void setFilename(const QString &filename);

    QString getFilenameNoPrefix() const;

    const QString &CoverFile() const;
    void setCoverFile(const QString &coverFile);

    const genre_list &Genres() const;
    void setGenres(const genre_list &genres);

    void dumpToDatabase();
    void updateDatabase();
//    bool fillDataFromID(const MetadataListManager &cache);
    bool fillDataFromFilename(const MetadataListManager &cache);

    // If you aren't ProgrammeList don't call this
    bool deleteFile();

    // drops the metadata from the DB
    bool dropFromDB();

    /// Resets to default metadata
    void Reset();

  private:
    class MetadataImp *m_imp;
};

bool operator==(const Metadata &a, const Metadata &b);
bool operator!=(const Metadata &a, const Metadata &b);

bool operator<(const Metadata::SortKey &lhs, const Metadata::SortKey &rhs);

#endif
